home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / encodings / __init__.pyc (.txt) next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  4.2 KB  |  136 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. ''' Standard "encodings" Package
  5.  
  6.     Standard Python encoding modules are stored in this package
  7.     directory.
  8.  
  9.     Codec modules must have names corresponding to normalized encoding
  10.     names as defined in the normalize_encoding() function below, e.g.
  11.     \'utf-8\' must be implemented by the module \'utf_8.py\'.
  12.  
  13.     Each codec module must export the following interface:
  14.  
  15.     * getregentry() -> codecs.CodecInfo object
  16.     The getregentry() API must a CodecInfo object with encoder, decoder,
  17.     incrementalencoder, incrementaldecoder, streamwriter and streamreader
  18.     atttributes which adhere to the Python Codec Interface Standard.
  19.  
  20.     In addition, a module may optionally also define the following
  21.     APIs which are then used by the package\'s codec search function:
  22.  
  23.     * getaliases() -> sequence of encoding name strings to use as aliases
  24.  
  25.     Alias names returned by getaliases() must be normalized encoding
  26.     names as defined by normalize_encoding().
  27.  
  28. Written by Marc-Andre Lemburg (mal@lemburg.com).
  29.  
  30. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  31.  
  32. '''
  33. import codecs
  34. import types
  35. from encodings import aliases
  36. _cache = { }
  37. _unknown = '--unknown--'
  38. _import_tail = [
  39.     '*']
  40. _norm_encoding_map = '                                              . 0123456789       ABCDEFGHIJKLMNOPQRSTUVWXYZ      abcdefghijklmnopqrstuvwxyz                                                                                                                                     '
  41. _aliases = aliases.aliases
  42.  
  43. class CodecRegistryError(LookupError, SystemError):
  44.     pass
  45.  
  46.  
  47. def normalize_encoding(encoding):
  48.     """ Normalize an encoding name.
  49.  
  50.         Normalization works as follows: all non-alphanumeric
  51.         characters except the dot used for Python package names are
  52.         collapsed and replaced with a single underscore, e.g. '  -;#'
  53.         becomes '_'. Leading and trailing underscores are removed.
  54.  
  55.         Note that encoding names should be ASCII only; if they do use
  56.         non-ASCII characters, these must be Latin-1 compatible.
  57.  
  58.     """
  59.     if type(encoding) is types.UnicodeType:
  60.         encoding = encoding.encode('latin-1')
  61.     
  62.     return '_'.join(encoding.translate(_norm_encoding_map).split())
  63.  
  64.  
  65. def search_function(encoding):
  66.     entry = _cache.get(encoding, _unknown)
  67.     if entry is not _unknown:
  68.         return entry
  69.     
  70.     norm_encoding = normalize_encoding(encoding)
  71.     if not _aliases.get(norm_encoding):
  72.         pass
  73.     aliased_encoding = _aliases.get(norm_encoding.replace('.', '_'))
  74.     if aliased_encoding is not None:
  75.         modnames = [
  76.             aliased_encoding,
  77.             norm_encoding]
  78.     else:
  79.         modnames = [
  80.             norm_encoding]
  81.     for modname in modnames:
  82.         if not modname:
  83.             continue
  84.         
  85.         
  86.         try:
  87.             mod = __import__('encodings.' + modname, globals(), locals(), _import_tail)
  88.         except ImportError:
  89.             continue
  90.  
  91.     else:
  92.         mod = None
  93.     
  94.     try:
  95.         getregentry = mod.getregentry
  96.     except AttributeError:
  97.         mod = None
  98.  
  99.     if mod is None:
  100.         _cache[encoding] = None
  101.         return None
  102.     
  103.     entry = getregentry()
  104.     if not isinstance(entry, codecs.CodecInfo):
  105.         if len(entry) <= len(entry):
  106.             pass
  107.         elif not len(entry) <= 7:
  108.             raise CodecRegistryError, 'module "%s" (%s) failed to register' % (mod.__name__, mod.__file__)
  109.         
  110.         if not not callable(entry[0]) and not callable(entry[1]):
  111.             if not entry[2] is not None or not callable(entry[2]):
  112.                 if not entry[3] is not None or not callable(entry[3]):
  113.                     if (len(entry) > 4 and entry[4] is not None or not callable(entry[4]) or len(entry) > 5) and entry[5] is not None and not callable(entry[5]):
  114.                         raise CodecRegistryError, 'incompatible codecs in module "%s" (%s)' % (mod.__name__, mod.__file__)
  115.                     
  116.         if len(entry) < 7 or entry[6] is None:
  117.             entry += (None,) * (6 - len(entry)) + (mod.__name__.split('.', 1)[1],)
  118.         
  119.         entry = codecs.CodecInfo(*entry)
  120.     
  121.     _cache[encoding] = entry
  122.     
  123.     try:
  124.         codecaliases = mod.getaliases()
  125.     except AttributeError:
  126.         pass
  127.  
  128.     for alias in codecaliases:
  129.         if not _aliases.has_key(alias):
  130.             _aliases[alias] = modname
  131.             continue
  132.     
  133.     return entry
  134.  
  135. codecs.register(search_function)
  136.